
package cn.sylinx.web.util;

import java.net.URL;

/**
 * StrKit.
 */
public class StringUtil {

	/**
	 * 首字母变小写
	 */
	public static String firstCharToLowerCase(String str) {
		char firstChar = str.charAt(0);
		if (firstChar >= 'A' && firstChar <= 'Z') {
			char[] arr = str.toCharArray();
			arr[0] += ('a' - 'A');
			return new String(arr);
		}
		return str;
	}

	/**
	 * 首字母变大写
	 */
	public static String firstCharToUpperCase(String str) {
		char firstChar = str.charAt(0);
		if (firstChar >= 'a' && firstChar <= 'z') {
			char[] arr = str.toCharArray();
			arr[0] -= ('a' - 'A');
			return new String(arr);
		}
		return str;
	}

	/**
	 * 字符串为 null 或者为 "" 时返回 true
	 */
	public static boolean isBlank(String str) {
		return str == null || "".equals(str.trim()) ? true : false;
	}

	/**
	 * 字符串不为 null 而且不为 "" 时返回 true
	 */
	public static boolean isNotBlank(String str) {
		return str == null || "".equals(str.trim()) ? false : true;
	}

	public static boolean isNotBlank(String... strings) {
		if (strings == null)
			return false;
		for (String str : strings)
			if (str == null || "".equals(str.trim()))
				return false;
		return true;
	}

	public static boolean isNotNull(Object... paras) {
		if (paras == null)
			return false;
		for (Object obj : paras)
			if (obj == null)
				return false;
		return true;
	}

	public static String getRootPath(URL url) {

		String fileUrl = url.getFile();

		int pos = fileUrl.indexOf("!");

		if (-1 == pos) {
			return fileUrl;
		}

		return fileUrl.substring(5, pos);
	}

	public static String dotToSplash(String name) {

		return name.replaceAll("\\.", "/");
	}

	public static String trimExtension(String name) {

		int pos = name.indexOf(".");
		if (-1 != pos) {
			return name.substring(0, pos);
		}

		return name;
	}

	public static String trimUrl(String url) {
		String trimed = url.substring(1);
		int splashIndex = trimed.indexOf('/');
		return trimed.substring(splashIndex);
	}
}
